package controllers

import (
	"crypto/sha1"
	"fmt"
	_ "fmt"
	"github.com/astaxie/beego"
	_ "github.com/astaxie/beego/session"
	"github.com/astaxie/beego/validation"
	"io"
	"teamcoop/action"
	. "teamcoop/global"
)

func mysha1(c string) (ret string) {

	h := sha1.New()
	io.WriteString(h, c)
	ret = fmt.Sprintf("%x", h.Sum(nil))
	return
}

type ActionController struct {
	beego.Controller
}

func (this *ActionController) Main() {

	this.TplNames = "main.html"
	sid := this.Ctx.GetCookie("sid")
	email, nick, class, _ := action.UserInfo(sid)
	this.Data["user_email"] = email
	this.Data["user_nick"] = nick
	this.Data["user_class"] = class
}

func (this *ActionController) Login() {

	this.TplNames = "login.html"
	this.Data["error_infoamation"] = ""
	this.Data["error_hide"] = "hide"
	var sid, nick string
	var err error = nil
	if this.GetString("exit") == "true" {

		beego.Info("receive : logout!!\n")
		this.Ctx.SetCookie("sid", "", 0)
		this.Ctx.SetCookie("email", "", 0)
		this.Ctx.SetCookie("pwd", "", 0)
		this.Ctx.SetCookie("who", "", 0)
		this.Ctx.SetCookie("nick", "", 0)
		this.Redirect("/", 301)
		return
	}

	valid := validation.Validation{}
	login := &FormLogin{}
	err = this.ParseForm(login)
	beego.Info("receive:login\t", login)
	if err != nil {
		beego.Error(err.Error())
		goto _error
	}
	_, err = valid.Valid(login)
	if err != nil {
		beego.Error(err.Error())
		goto _error
	}
	sid, err = action.UserLogin(login.Email, login.Pwd)
	if err != nil {
		beego.Error(err.Error())
		goto _error
	}

	this.Ctx.SetCookie("sid", sid)
	this.Ctx.SetCookie("who", login.Email)
	this.Ctx.SetCookie("email", login.Email, 1<<31-1, "/")
	_, nick, _, _ = action.UserInfo(sid)
	this.Ctx.SetCookie("nick", nick)
	if login.AutoLogin == "on" {
		this.Ctx.SetSecureCookie("jbl's teamcoop", "pwd", login.Pwd, 1<<31-1, "/")
	}
_error:
	if err != nil {
		this.Data["error_hide"] = ""
		this.Data["error_infoamation"] = err.Error()
	}

	this.Redirect("/", 301)
	return

}

func (this *ActionController) Todo() {
	var err error = nil
	var id int64 = -1
	beego.Info(this.Ctx.Input)
	switch this.Ctx.Input.Method() {
	case "POST":
		{
			this.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
			switch this.GetString("action") {
			case "add":
				{
					sid := this.Ctx.GetCookie("sid")
					text := this.GetString("text")
					level, _ := this.GetInt("level")
					beego.Info("todo add: \n text", text, "\n level", int8(level))
					id, err = action.TodoAdd(sid, text, int8(level))
					if err != nil {
						beego.Error(err.Error())
					}
					break
				}
			case "remove":
				{
					sid := this.Ctx.GetCookie("sid")
					id, _ = this.GetInt("id")
					err = action.TodoRemove(sid, id)
					if err != nil {
						beego.Error(err.Error())
					}
					this.Ctx.Output.Body([]byte(string(id)))
					break
				}
			case "done":
				{
					sid := this.Ctx.GetCookie("sid")
					id, _ = this.GetInt("id")
					err = action.TodoDone(sid, id)
					if err != nil {
						beego.Error(err.Error())
					}
					this.Ctx.Output.Body([]byte(string(id)))
					break
				}
			case "undone":
				{
					sid := this.Ctx.GetCookie("sid")
					id, _ = this.GetInt("id")
					err = action.TodoUndone(sid, id)
					if err != nil {
						beego.Error(err.Error())
					}
					this.Ctx.Output.Body([]byte(string(id)))
					break
				}
			case "alldone":
				{
					sid := this.Ctx.GetCookie("sid")
					err = action.TodoAllDone(sid)
					if err != nil {
						beego.Error(err.Error())
					}
					break
				}
			case "allundone":
				{
					sid := this.Ctx.GetCookie("sid")
					err = action.TodoAllUnDone(sid)
					if err != nil {
						beego.Error(err.Error())
					}
					break
				}
			case "allremovedone":
				{
					sid := this.Ctx.GetCookie("sid")
					err = action.TodoAllRemoveDone(sid)
					if err != nil {
						beego.Error(err.Error())
					}
					break
				}
			}
			this.Ctx.Output.Body([]byte(string(id)))

		}
	case "GET":
		{
			this.TplNames = "todo_list.tpl"
			switch this.GetString("action") {
			case "load":
				{
					sid := this.Ctx.GetCookie("sid")
					todoes_undone, err := action.TodoListUndone(sid)
					if err != nil {
						beego.Error(err.Error())
					}
					todoes_done, err := action.TodoListDone(sid)
					if err != nil {
						beego.Error(err.Error())
					}
					beego.Info("todoes_undone num: ", len(todoes_undone))
					beego.Info("todoes_done num: ", len(todoes_done))
					this.Data["todoes_undone"] = todoes_undone
					this.Data["todoes_done"] = todoes_done
					break
				}
			case "look":
				{
					sid := this.Ctx.GetCookie("sid")
					id, _ := this.GetInt("id")
					todoes_undone, err := action.TodoListUndoneOther(sid, id)
					if err != nil {
						beego.Error(err.Error())
					}
					todoes_done, err := action.TodoListDoneOther(sid, id)
					if err != nil {
						beego.Error(err.Error())
					}
					beego.Info("todoes_undone num: ", len(todoes_undone))
					beego.Info("todoes_done num: ", len(todoes_done))
					this.Data["todoes_undone"] = todoes_undone
					this.Data["todoes_done"] = todoes_done
					break
				}
			}

		}
	}
	return
}
func (this *ActionController) User() {

	var err error = nil
	var id int64
	var sid string
	beego.Info(this.Ctx.Input)
	switch this.Ctx.Input.Method() {
	case "POST":
		{
			this.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
			switch this.GetString("action") {
			case "set":
				{
					sid := this.Ctx.GetCookie("sid")
					nick := this.GetString("nick")
					class, _ := this.GetInt("class")
					target, _ := this.GetInt("target")
					beego.Info("\nUser set:\nnick:", nick, "\nclass:", class, "\ntarget:", target)
					err = action.UserSetNick(sid, target, nick)
					ret := "true"
					if err != nil {
						beego.Error(err.Error())
						ret = "false"
					}
					err = action.UserSetClass(sid, target, int8(class))
					if err != nil {
						beego.Error(err.Error())
						ret = "false"
					}

					this.Ctx.Output.Body([]byte(ret))
					break
				}
			case "remove":
				{
					sid := this.Ctx.GetCookie("sid")
					id, err := this.GetInt("id")

					err = action.UserRemove(sid, id)
					if err != nil {
						beego.Error(err.Error())
						this.Ctx.Output.Body([]byte("false"))
						break
					}
					this.Ctx.Output.Body([]byte("true"))
					break
				}
			}
			break
		}
	case "GET":
		{
			switch this.GetString("action") {
			case "load":
				{
					this.TplNames = "users_list.tpl"
					sid := this.Ctx.GetCookie("sid")
					uesrs_list, err := action.UserList(sid)
					if err != nil {
						beego.Error(err.Error())
						break
					}
					this.Data["uesrs_list"] = uesrs_list
					break
				}
			case "modify":
				{
					this.TplNames = "user_modify.tpl"
					sid = this.Ctx.GetCookie("sid")
					id, _ = this.GetInt("id")
					email, nick, class, err := action.UserInfo(sid)
					if err != nil {
						beego.Error(err.Error())
						break
					}
					this.Data["target_id"] = id
					this.Data["target_email"] = email
					this.Data["target_nick"] = nick
					this.Data["target_class"] = class
					break
				}
			}
			break
		}
	}
}
func (this *ActionController) Comment() {

	var err error = nil
	var cid, id int64
	var sid, text string
	beego.Info(this.Ctx.Input)
	switch this.Ctx.Input.Method() {
	case "POST":
		{
			id, _ = this.GetInt("id")
			text = this.GetString("text")
			this.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
			switch this.GetString("action") {
			case "add":
				{
					switch this.GetString("type") {
					case "todo":
						{
							sid = this.Ctx.GetCookie("sid")
							cid, err = action.CommentTodoAdd(sid, id, text)
							if err != nil {
								beego.Error(err.Error())
							}
							this.Ctx.Output.Body([]byte(string(cid)))
							break
						}
					case "announce":
						{
							sid = this.Ctx.GetCookie("sid")
							cid, err = action.CommentAnnounceAdd(sid, id, text)
							if err != nil {
								beego.Error(err.Error())
							}

							this.Ctx.Output.Body([]byte(string(cid)))
							break
						}
					}
				}
			case "remove":
				{
					sid = this.Ctx.GetCookie("sid")
					err = action.CommentRemove(sid, id)
					if err != nil {
						beego.Error(err.Error())
					}
					break
				}
			}
			break
		}
	case "GET":
		{
			this.TplNames = "comment_list.tpl"
			id, _ := this.GetInt("id")
			switch this.GetString("action") {
			case "todo":
				{
					sid = this.Ctx.GetCookie("sid")
					comments, err := action.CommentListTodo(sid, id)
					if err != nil {
						beego.Error(err.Error())
					}
					this.Data["comments"] = comments
					break
				}
			case "announce":
				{
					sid = this.Ctx.GetCookie("sid")
					comments, err := action.CommentListAnnounce(sid, id)
					if err != nil {
						beego.Error(err.Error())
					}
					this.Data["comments"] = comments
					break
				}
			}
			break
		}
	}
}
func (this *ActionController) Dynamic() {

	//var err error = nil
	//var cid, id int64
	var sid string
	beego.Info(this.Ctx.Input)
	switch this.Ctx.Input.Method() {
	case "GET":
		{
			// this.TplNames = "dynamic.tpl"
			switch this.GetString("action") {
			case "dynamic":
				{
					sid = this.Ctx.GetCookie("sid")
					dynamic, err := action.DynamicList(sid)
					if err != nil {
						beego.Error(err.Error())
					}
					this.Data["announce_list"] = dynamic

					break
				}

			}
			break
		}
	}
}
func (this *ActionController) Announce() {

	var err error = nil
	var id int64
	var sid string
	beego.Info(this.Ctx.Input)
	switch this.Ctx.Input.Method() {
	case "POST":
		{
			this.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
			switch this.GetString("action") {
			case "add":
				{

					sid := this.Ctx.GetCookie("sid")
					text := this.GetString("text")
					beego.Info("todo add: \n text", text)
					id, err := action.AnnouncePublish(sid, text)
					if err != nil {
						beego.Error(err.Error())
					}
					this.Ctx.Output.Body([]byte(string(id)))
					break
				}
			case "remove":
				{
					sid := this.Ctx.GetCookie("sid")
					id, _ = this.GetInt("id")
					err = action.AnnounceRemove(sid, id)
					if err != nil {
						beego.Error(err.Error())
					}
					this.Ctx.Output.Body([]byte(string("")))
					break
				}
			}
			this.Ctx.Output.Body([]byte("true"))
			break
		}
	case "GET":
		{
			this.TplNames = "dynamic_list.tpl"
			switch this.GetString("action") {
			case "dynamic":
				{
					sid = this.Ctx.GetCookie("sid")
					announce_list, err := action.AnnounceList(sid)
					if err != nil {
						beego.Error(err.Error())
					}
					beego.Info("announce_list num: ", len(announce_list))
					this.Data["announce_list"] = announce_list

					dynamic_list, err := action.DynamicList(sid)
					if err != nil {
						beego.Error(err.Error())
					}
					beego.Info("dynamic_list num: ", len(dynamic_list))
					this.Data["dynamic_list"] = dynamic_list
					break
				}
			}
			break
		}
	}
}
